#include <thread>
#include <sys/time.h>

#include "WlanService.h"
#include "ConfigApi.h"
#include "CurtainActManager.h"
#include "ScreenMirrorStream.h"
#include "DiskController.h"
#include "Pal/Net.h"
#include "GuiDisplay.h"
#include "DailySchedule.h"
#include "Util/File.h"


extern "C" int showOsdInfo();

using namespace ConfigApi;
extern "C"{
    uint32_t custom_tick_get(void)
    {
        static uint64_t start_ms = 0;
        if(start_ms == 0) {
            struct timeval tv_start;
            gettimeofday(&tv_start, NULL);
            start_ms = (tv_start.tv_sec * 1000000 + tv_start.tv_usec) / 1000;
        }

        struct timeval tv_now;
        gettimeofday(&tv_now, NULL);
        uint64_t now_ms;
        now_ms = (tv_now.tv_sec * 1000000 + tv_now.tv_usec) / 1000;

        uint32_t time_ms = now_ms - start_ms;
        return time_ms;
    }

    void DisplayDefaultDigitalSignage(){
        Media::GuiDisplay::Instance()->DisplayDefaultDigitalSignage();
    }

    void DisplayOsd(){
        Media::GuiDisplay::Instance()->DisplayOsd();
    }
}

namespace Media{
    std::string default_img = "A:/user/data/StreamFile/PictureFile/startup.png";
    void obj_event_cb(lv_event_t * e){
        // printf("event code %d \n", lv_event_get_code(e));
        GuiDisplay::Instance()->SwitchDisplay();
        // GuiDisplay *gui = lv_event_get_user_data(e);
        // if(gui){
        //     GuiDisplay::Instance()->SwitchDisplay();
        // }else{
        //     printf("gui   is  NULL !!!\n");
        // }
    }

    void img_event_cb(lv_event_t * e){
        GuiDisplay::Instance()->Update();
    }

    GuiDisplay::Ptr GuiDisplay::Instance(){
        static auto ptr = std::make_shared<GuiDisplay>();
        return ptr;
    }
    GuiDisplay::GuiDisplay(){
        init();
    }
    GuiDisplay::~GuiDisplay(){

    }

    void GuiDisplay::ReloadHomeScreen(){
        
        Json::Value homeScreen;
   
        // homeScreen["homeScreenImage"] = fileName;
        // ConfigApi::IConfigInterface::GetInstance()->Set("homeScreen",
        //  homeScreen, ConfigApi::ConfigOptions::ConfigOptionPart);

        m_reloadImg = true;
        if(m_state == Default){
            DisplayDefaultDigitalSignage();
        }
    }

    void GuiDisplay::SwitchDisplay(){
        tracef("!!!!!!!!!!! SwitchDisplay  m_state %d", m_state);
        if(m_state == Default){
            // lv_obj_set_style_bg_opa(lv_scr_act(), LV_OPA_COVER, 0);  //LV_OPA_COVER
            // lv_obj_set_style_bg_opa(m_osd_lb, LV_OPA_TRANSP, 0);
            
            if(m_osd_lb) lv_obj_add_flag(m_osd_lb, LV_OBJ_FLAG_HIDDEN);
            if(m_obj) lv_obj_clear_flag(m_obj, LV_OBJ_FLAG_HIDDEN);

            if(m_reloadImg){
                m_reloadImg = false;
                Json::Value table = Json::nullValue;    
                IConfigInterface::GetInstance()->Get("homeScreen", table);
                std::string img_path = default_img;
                if(table != Json::nullValue && table.isMember("homeScreenImage")){
                    std::string file_path = table["homeScreenImage"].asString();
                    if(!file_path.empty()){
                        if(Fdt::File::fileExist(file_path.c_str())){
                            img_path = "A:/"  + file_path;
                            if(m_share_code_lb){
                                lv_obj_add_flag(m_share_code_lb, LV_OBJ_FLAG_HIDDEN);
                            }
                            if(m_device_name_lb){
                                lv_obj_add_flag(m_device_name_lb, LV_OBJ_FLAG_HIDDEN);
                            }
                            if(m_ap_name_lb){
                                lv_obj_add_flag(m_ap_name_lb, LV_OBJ_FLAG_HIDDEN);
                            }
                            if(m_password_lb){
                                lv_obj_add_flag(m_password_lb, LV_OBJ_FLAG_HIDDEN);
                            }
                            if(m_wifi_lb){
                                lv_obj_add_flag(m_wifi_lb, LV_OBJ_FLAG_HIDDEN);
                            }
                            if(m_eth_ip_lb){
                                lv_obj_add_flag(m_eth_ip_lb, LV_OBJ_FLAG_HIDDEN);
                            }
                            if(m_actived_lb){
                                lv_obj_add_flag(m_actived_lb, LV_OBJ_FLAG_HIDDEN);
                            }
                        }
                    }
                }

                if(img_path == default_img){
                    if(m_share_code_lb){
                        lv_obj_clear_flag(m_share_code_lb, LV_OBJ_FLAG_HIDDEN);
                    }
                    if(m_device_name_lb){
                        lv_obj_clear_flag(m_device_name_lb, LV_OBJ_FLAG_HIDDEN);
                    }
                    if(m_ap_name_lb){
                        lv_obj_clear_flag(m_ap_name_lb, LV_OBJ_FLAG_HIDDEN);
                    }
                    if(m_password_lb){
                        lv_obj_clear_flag(m_password_lb, LV_OBJ_FLAG_HIDDEN);
                    }
                    if(m_wifi_lb){
                        lv_obj_clear_flag(m_wifi_lb, LV_OBJ_FLAG_HIDDEN);
                    }
                    if(m_eth_ip_lb){
                        lv_obj_clear_flag(m_eth_ip_lb, LV_OBJ_FLAG_HIDDEN);
                    }
                    if(m_actived_lb){
                        lv_obj_clear_flag(m_actived_lb, LV_OBJ_FLAG_HIDDEN);
                    }
                }

                tracef(" homescreen img is %s\n", img_path.c_str());
                lv_img_set_src(m_obj, img_path.c_str());
                lv_event_send(m_obj, LV_EVENT_DRAW_POST, this);
            }
            // lv_img_set_src(m_obj, "A:/user/data/StreamFile/PictureFile/startup.jpg");
            // lv_event_send(m_obj, LV_EVENT_DRAW_POST, this);
        }else if(m_state == Osd){
            // lv_disp_clean_dcache(nullptr);

            lv_obj_set_style_bg_opa(m_obj, LV_OPA_TRANSP, 0);
            // lv_obj_set_style_bg_opa(m_osd_lb, LV_OPA_50, 0);

            if(m_osd_lb) lv_obj_clear_flag(m_osd_lb, LV_OBJ_FLAG_HIDDEN);
            if(m_obj) lv_obj_add_flag(m_obj, LV_OBJ_FLAG_HIDDEN);
            

            // lv_img_set_src(m_obj, nullptr);
            // lv_event_send(m_obj, LV_EVENT_DRAW_POST, this);
            // lv_obj_add_state(m_obj, LV_STATE_DISABLED);
        }else if(m_state == HideOsd){
            if(m_osd_lb) lv_obj_add_flag(m_osd_lb, LV_OBJ_FLAG_HIDDEN);
            if(m_obj) lv_obj_add_flag(m_obj, LV_OBJ_FLAG_HIDDEN);
        }
    }
    void GuiDisplay::loadDisplayInfo(bool& needUpdate){
        
        Json::Value nameTable;
        IConfigInterface::GetInstance()->Get("DevName", nameTable);
        if(nameTable.isMember("Name") && nameTable["Name"].isString()){
            m_osd_info.m_device_name = nameTable["Name"].asString();
        }else{
            m_osd_info.m_device_name = "";
        }

        std::string devicePassword = Media::ScreenMirrorStream::instance()->getdevicePassword();
        std::string spacePwd;
        for (size_t i = 0; i < devicePassword.length(); i++) {
            spacePwd += devicePassword.substr(i, 1) + "   ";
        }
        m_osd_info.m_share_code = spacePwd;


        std::string apInfo;
        Media::WlanService::instance()->getApInfo(m_osd_info.m_ap_name, m_osd_info.m_ap_password);
        
        Media::WlanService::instance()->getStaInfo(m_osd_info.m_wifi);
        if(m_osd_info.m_wifi.empty()){
            m_osd_info.m_wifi = "No connected";
        }

        Json::Value ipTable;
        bool result =  Media::WlanService::instance()->getWiredNetwork(ipTable);
        if(mii_diag(LAN_ETH0) == 0 && result && ipTable.isMember("IP")){
            m_osd_info.m_eth_ip = ipTable["IP"].asString();
        }else{
            m_osd_info.m_eth_ip = "No connected";
        }

        if(!Media::ScreenMirrorStream::instance()->isActived()){
            m_osd_info.m_actived = false;
        }else{
            m_osd_info.m_actived = true;
        }

        if(m_device_name_lb_text != m_osd_info.m_device_name ||
            m_share_code_lb_text != m_osd_info.m_share_code ||
            m_ap_name_lb_text != "AP: " + m_osd_info.m_ap_name ||
            m_password_lb_text != "Password: " + m_osd_info.m_ap_password ||
            m_wifi_lb_text != "Wi-Fi: " + m_osd_info.m_wifi ||
            m_eth_ip_lb_text != "Ethernet: " + m_osd_info.m_eth_ip ||
            m_actived != m_osd_info.m_actived){
            needUpdate = true;
        }else{
            needUpdate = false;
        }

        m_device_name_lb_text = m_osd_info.m_device_name;
        m_share_code_lb_text = m_osd_info.m_share_code;
        m_ap_name_lb_text = "AP:" + m_osd_info.m_ap_name;
        m_password_lb_text = "Password:" + m_osd_info.m_ap_password;
        m_wifi_lb_text = "Wi-Fi:" + m_osd_info.m_wifi;
        m_eth_ip_lb_text = "Ethernet:" + m_osd_info.m_eth_ip;
        m_actived = m_osd_info.m_actived;
        std::string share_code;
        for(char c : m_osd_info.m_share_code){
            if(c != ' '){
                //char_flag = true;
                share_code += c;
            }
        }
        m_osd_lb_text =  "Sharing Code:" + share_code + "  " + m_ap_name_lb_text + "  " + m_password_lb_text + "  Name:" + m_device_name_lb_text +"  " + m_wifi_lb_text;
    }
    
    void log_print_g_cb_t(const char * buf){
        // printf("%s", buf);
    }

    void GuiDisplay::init(){
        lv_init();
        // lv_log_register_print_cb(log_print_g_cb_t);
        lv_disp_draw_buf_init(&m_disp_buf, m_buf, nullptr, DISP_BUF_SIZE);
        lv_disp_drv_init(&m_disp_drv);
        m_disp_drv.draw_buf   = &m_disp_buf;
        m_disp_drv.flush_cb   = rk_media_flush;
        m_disp_drv.hor_res    = LV_WIDTH;
        m_disp_drv.ver_res    = LV_HEIGHT;
        m_disp_drv.full_refresh = 1;
        lv_disp_drv_register(&m_disp_drv);

        //
        lv_obj_set_style_bg_opa(lv_scr_act(), LV_OPA_TRANSP, 0);  //LV_OPA_TRANSP


        // m_obj = lv_obj_create(lv_scr_act());
        // lv_obj_set_size(m_obj, 1920, 1080);
        // lv_obj_set_style_bg_img_src(m_obj, "A:/user/data/StreamFile/PictureFile/startup.jpg", 0);

        // lv_obj_set_style_bg_color(m_obj, lv_color_hex(0x3E4059), 0);

        //lv_obj_create(lv_scr_act());
        fontInit();

        m_obj = lv_img_create(lv_scr_act());
        lv_obj_set_size(m_obj, 1920, 1080);

        Json::Value table = Json::nullValue;    
	    if(!IConfigInterface::GetInstance()->Get("homeScreen", table)){
            warnf("not found homeScreen  config");
        }
        std::string img_path = default_img;
        if(table != Json::nullValue && table.isMember("homeScreenImage")){
            std::string file_path = table["homeScreenImage"].asString();
            tracef("imageName  %s", file_path.c_str());
            if(!file_path.empty()){
                if(Fdt::File::fileExist(file_path.c_str())){
                    img_path = "A:/" + file_path;
                }
            }
        }

        tracef(" image path %s", img_path.c_str());
        lv_img_set_src(m_obj, img_path.c_str());

        lv_obj_set_style_bg_opa(m_obj, LV_OPA_50, 0);

        lv_obj_add_event_cb(lv_scr_act(), obj_event_cb, LV_EVENT_REFRESH, this);

        lv_obj_add_event_cb(m_obj, img_event_cb, LV_EVENT_REFRESH, this);

        bool needUpdate = false;
        loadDisplayInfo(needUpdate);

        //device name
        lv_style_init(&m_device_name_lb_style);
        m_device_name_lb = create_custom_label(m_obj, m_osd_info.m_device_name.c_str(), 
            &m_device_name_lb_style, m_name_font_info.font, 443, 220);
        // lv_obj_set_style_bg_opa(m_device_name_lb, LV_OPA_50, 0);

        //sharing code
        lv_style_init(&m_share_code_lb_style);
        m_share_code_lb = create_custom_label(m_obj, m_osd_info.m_share_code.c_str(), &m_share_code_lb_style, m_default_font_info.font, 443, 340);
        // lv_obj_set_style_bg_opa(m_share_code_lb, LV_OPA_50, 0);

        //AP name
        lv_style_init(&m_ap_name_lb_style);
        m_ap_name_lb = create_custom_label(m_obj, m_ap_name_lb_text.c_str(), &m_ap_name_lb_style, m_default_font_info.font, 443, 460);
        // lv_obj_set_style_bg_opa(m_ap_name_lb, LV_OPA_50, 0);

        //Password
        lv_style_init(&m_password_lb_style);
        m_password_lb = create_custom_label(m_obj, m_password_lb_text.c_str(), &m_password_lb_style, m_default_font_info.font, 443, 500);

        //wifi name
        lv_style_init(&m_wifi_lb_style);
        m_wifi_lb = create_custom_label(m_obj, m_wifi_lb_text.c_str(), &m_wifi_lb_style, m_default_font_info.font, 443, 540);

        //Ethernet
        lv_style_init(&m_eth_ip_lb_style);
        m_eth_ip_lb = create_custom_label(m_obj, m_eth_ip_lb_text.c_str(), &m_eth_ip_lb_style, m_default_font_info.font, 443, 580);

        //actived
        std::string active_message = "";
        if(!m_actived){
            active_message = "no_active";
        }
        lv_style_init(&m_actived_lb_style);
        m_actived_lb = create_custom_label(m_obj, active_message.c_str(), &m_actived_lb_style, m_default_font_info.font, 1290, 233);
        
        //play osd
        lv_style_init(&m_osd_lb_style);
        m_osd_lb = create_custom_label(lv_scr_act(), 
            m_osd_lb_text.c_str(),
            &m_osd_lb_style, m_osd_font_info.font, 395, -5, lv_color_hex(0x000000));
                
        //背景颜色透明度
        lv_obj_set_style_bg_opa(m_osd_lb, LV_OPA_30, 0);
        //设置内边距和文字居中
        lv_obj_set_width(m_osd_lb, LV_SIZE_CONTENT);
        lv_obj_set_style_pad_hor(m_osd_lb, 40, 0);
        lv_style_set_pad_top(&m_osd_lb_style, 20);
        lv_style_set_pad_bottom(&m_osd_lb_style, 12);
        lv_style_set_text_align(&m_osd_lb_style, LV_TEXT_ALIGN_CENTER);
        // 设置行高
        lv_style_set_text_line_space(&m_osd_lb_style, 26); // 设置行高为26px

        // 设置对象样式，包括背景颜色和底部圆角边框
        lv_style_set_radius(&m_osd_lb_style, 16); // 底部圆角
        // 将样式应用于对象
        lv_obj_add_style(m_osd_lb, &m_osd_lb_style, LV_PART_MAIN);
        //设置窗体水平居中
        lv_obj_align(m_osd_lb, LV_ALIGN_TOP_MID, 0, -8);
        
        lv_obj_add_flag(m_osd_lb, LV_OBJ_FLAG_HIDDEN);
        lv_obj_add_flag(m_obj, LV_OBJ_FLAG_HIDDEN);


        if(img_path != default_img){
            if(m_share_code_lb){
                lv_obj_add_flag(m_share_code_lb, LV_OBJ_FLAG_HIDDEN);
            }
            if(m_device_name_lb){
                lv_obj_add_flag(m_device_name_lb, LV_OBJ_FLAG_HIDDEN);
            }
            if(m_ap_name_lb){
                lv_obj_add_flag(m_ap_name_lb, LV_OBJ_FLAG_HIDDEN);
            }
            if(m_password_lb){
                lv_obj_add_flag(m_password_lb, LV_OBJ_FLAG_HIDDEN);
            }
            if(m_wifi_lb){
                lv_obj_add_flag(m_wifi_lb, LV_OBJ_FLAG_HIDDEN);
            }
            if(m_eth_ip_lb){
                lv_obj_add_flag(m_eth_ip_lb, LV_OBJ_FLAG_HIDDEN);
            }
            if(m_actived_lb){
                lv_obj_add_flag(m_actived_lb, LV_OBJ_FLAG_HIDDEN);
            }
        }

        std::thread th([this](){
            this->run();
        });
        th.detach();
    }

    void GuiDisplay::fontInit(){
         /*Create a font*/
        /*FreeType uses C standard file system, so no driver letter is required.*/
        m_osd_font_info.name = "/user/data/osd/siyuan.ttf";
        m_osd_font_info.weight = 26; 
        m_osd_font_info.style = FT_FONT_STYLE_NORMAL;
        m_osd_font_info.mem = NULL;
        if(!lv_ft_font_init(&m_osd_font_info)) {
            LV_LOG_ERROR("create failed.");
        }
        /*Create style with the new font*/
        static lv_style_t style;
        lv_style_init(&style);
        lv_style_set_text_font(&style, m_osd_font_info.font);


        /*Create a font*/
        /*FreeType uses C standard file system, so no driver letter is required.*/
        m_name_font_info.name = "/user/data/osd/siyuan.ttf";
        m_name_font_info.weight = 35; 
        m_name_font_info.style = FT_FONT_STYLE_NORMAL;
        m_name_font_info.mem = NULL;
        if(!lv_ft_font_init(&m_name_font_info)) {
            LV_LOG_ERROR("create failed.");
        }
        /*Create style with the new font*/
        static lv_style_t name_style;
        lv_style_init(&name_style);
        lv_style_set_text_font(&name_style, m_name_font_info.font);

        /*Create a font*/
        /*FreeType uses C standard file system, so no driver letter is required.*/
        m_default_font_info.name = "/user/data/osd/siyuan.ttf";
        m_default_font_info.weight = 22;   
        m_default_font_info.style = FT_FONT_STYLE_NORMAL;
        m_default_font_info.mem = NULL;
        if(!lv_ft_font_init(&m_default_font_info)) {
            LV_LOG_ERROR("create failed.");
        }

        /*Create style with the new font*/
        static lv_style_t style_default;
        lv_style_init(&style_default);
        lv_style_set_text_font(&style, m_default_font_info.font);

    }
    

    void GuiDisplay::SetDisplayInfo(const std::string& device_name, const std::string& share_code, 
        const std::string& ap_name, const std::string& password, const std::string& wifi,
        const std::string& eth_ip){
        // DisplayInfo info;
        m_osd_info.m_device_name = device_name;
        m_osd_info.m_share_code = share_code;
        m_osd_info.m_ap_name = ap_name;
        m_osd_info.m_ap_password = password;
        m_osd_info.m_wifi = wifi;
        m_osd_info.m_eth_ip = eth_ip;

        lv_event_send(m_obj, LV_EVENT_REFRESH, this);
    }

    void GuiDisplay::Update(){
        bool needUpdate = false;
        loadDisplayInfo(needUpdate);

        if(!needUpdate) return;


        //tracef("GuiDisplay Update");
        if(m_device_name_lb == nullptr){
            lv_style_init(&m_device_name_lb_style);
            m_device_name_lb = create_custom_label(m_obj, m_device_name_lb_text.c_str(), &m_device_name_lb_style, m_name_font_info.font, 443, 220);
        }else{
            lv_label_set_text(m_device_name_lb, m_device_name_lb_text.c_str());
        }
        
        if(m_share_code_lb == nullptr){
            lv_style_init(&m_share_code_lb_style);
            m_share_code_lb = create_custom_label(m_obj, m_share_code_lb_text.c_str(), &m_share_code_lb_style, m_default_font_info.font, 443, 340);
        }else{
            lv_label_set_text(m_share_code_lb, m_share_code_lb_text.c_str());
        }

        if(m_ap_name_lb == nullptr){
            lv_style_init(&m_ap_name_lb_style);
            m_ap_name_lb = create_custom_label(m_obj, m_ap_name_lb_text.c_str(), &m_ap_name_lb_style, m_default_font_info.font, 443, 460);
        }else{
            lv_label_set_text(m_ap_name_lb, m_ap_name_lb_text.c_str());
        }
        
        if(m_password_lb == nullptr){
            lv_style_init(&m_password_lb_style);
            m_password_lb = create_custom_label(m_obj, m_password_lb_text.c_str(), &m_password_lb_style, m_default_font_info.font, 443, 500);
        }else{
            lv_label_set_text(m_password_lb, m_password_lb_text.c_str());
        }

        if(m_wifi_lb == nullptr){
            lv_style_init(&m_wifi_lb_style);
            m_wifi_lb = create_custom_label(m_obj, m_wifi_lb_text.c_str(), &m_wifi_lb_style, m_default_font_info.font, 443, 540);
        }else{
            lv_label_set_text(m_wifi_lb, m_wifi_lb_text.c_str());
        }

        if(m_eth_ip_lb == nullptr){
           lv_style_init(&m_eth_ip_lb_style);
           m_eth_ip_lb = create_custom_label(m_obj, m_eth_ip_lb_text.c_str(), &m_eth_ip_lb_style, m_default_font_info.font, 443, 580);
        }else{
            lv_label_set_text(m_eth_ip_lb, m_eth_ip_lb_text.c_str());
        }

        if(m_actived_lb == nullptr){
           lv_style_init(&m_actived_lb_style);
           m_actived_lb = create_custom_label(m_obj, m_eth_ip_lb_text.c_str(), &m_actived_lb_style, m_default_font_info.font, 1290, 233);
        }else{
            std::string active_message = "";
            if(!m_actived){
                active_message = "no_active";
            }
            lv_label_set_text(m_actived_lb, active_message.c_str());
        }

        if(m_osd_lb == nullptr){
            lv_style_init(&m_osd_lb_style);
            m_osd_lb = create_custom_label(m_obj, m_osd_lb_text.c_str(), &m_eth_ip_lb_style, m_osd_font_info.font, 443, 580);
        }else{
            lv_label_set_text(m_osd_lb, m_osd_lb_text.c_str());
        }
    }


    void GuiDisplay::DisplayDefaultDigitalSignage(){
        if(m_state == Default && !m_reloadImg) return;
        tracef("DisplayDefaultDigitalSignage");
        m_state = Default;

        lv_event_send(lv_scr_act(), LV_EVENT_REFRESH, this);
    }
    void GuiDisplay::DisplayOsd(){
        int showStatus = showOsdInfo();
        if(m_state == Osd && showStatus == 0) return;
        if(m_state == HideOsd && showStatus != 0) return;

        tracef("DisplayOsd");
        if(showStatus){
            m_state = HideOsd;
        }else{
            m_state = Osd;
        }
        
        lv_event_send(lv_scr_act(), LV_EVENT_REFRESH, this);
    }

    bool GuiDisplay::LedStatusWarn(){
        return m_warnSatus.load() & USBDISKWARN;
    }

    void GuiDisplay::DisplayWarnSD(){
        m_warnSatusChange.store(true);
        m_warnSatus.fetch_or(USBDISKWARN);
        LedColor_t color;
        color.r = 0;
        color.g = 255;
        color.b = 0;
        LedCnt::instance()->setFlickLed(color, 500, 500);
    }

    void GuiDisplay::CloseDisplayWarnSD(){
        m_warnSatusChange.store(true);
        m_warnSatus.fetch_and(~USBDISKWARN);
        LedColor_t color;
        color.r = 0;
        color.g = 255;
        color.b = 0;
        LedCnt::instance()->setStaticLed(color);
    }

    void GuiDisplay::DisplayWarnWifi(){
        m_warnSatusChange.store(true);
        m_warnSatus.fetch_or(WIFIWARN);
    }

    void GuiDisplay::CloseDisplayWarnWifi(){
        m_warnSatusChange.store(true);
        m_warnSatus.fetch_and(~WIFIWARN);
    }

    void GuiDisplay::showWarnPrompt(){
        if(m_warnSatusChange.load()){
            m_warnSatusChange.store(false);
        }else{
            return false;
        }
        //首先删除之前显示的
        if (m_sd_error_lb != nullptr) {
            lv_obj_del(m_sd_error_lb);
            m_sd_error_lb = nullptr;
        }
        if (m_wifi_error_lb != nullptr) {
            lv_obj_del(m_wifi_error_lb);
            m_wifi_error_lb = nullptr;
        }
        
        //根据标志重新添加要渲染的警告
        if((m_warnSatus.load() & USBDISKWARN) && m_sd_error_lb == nullptr){
            m_sd_error_lb = lv_img_create(lv_scr_act());
            lv_obj_set_size(m_sd_error_lb, 1104, 48);
            lv_img_set_src(m_sd_error_lb, "A:/user/data/StreamFile/PictureFile/uDiskError.png");
            lv_obj_align(m_sd_error_lb, LV_ALIGN_BOTTOM_MID, 0, -60);
        }
    
        if((m_warnSatus.load() & WIFIWARN) && m_wifi_error_lb == nullptr){
            m_wifi_error_lb = lv_img_create(lv_scr_act());
            lv_obj_set_size(m_wifi_error_lb, 495, 48);
            lv_img_set_src(m_wifi_error_lb, "A:/user/data/StreamFile/PictureFile/wifiError.png");
            int upwardSpace = -60;
            if(m_warnSatus.load() & USBDISKWARN){
                upwardSpace -= 60;
            }
            lv_obj_align(m_wifi_error_lb, LV_ALIGN_BOTTOM_MID, 0, upwardSpace);
        }
    }

    lv_obj_t * GuiDisplay::create_custom_label(lv_obj_t * parent, const char * text, 
        lv_style_t* style_font, lv_font_t* font, 
        lv_coord_t x, lv_coord_t y, lv_color_t color){
        lv_obj_t * label = lv_label_create(parent);
         // lv_obj_set_style_bg_opa(label, LV_OPA_50, 0);

        // lv_obj_set_size(label, 1200, 50);
        lv_style_set_bg_color(&m_osd_lb_style, color);
        lv_label_set_text_fmt(label, text);
        
        // lv_style_t style_font;
        // const lv_font_t * font_large;
        // lv_style_init(&style_font);
        // font_large = font;
        lv_style_set_text_font(style_font, font);
        lv_style_set_text_color(style_font, lv_color_white());
        

        lv_obj_add_style(label, style_font, 0);
        // lv_obj_center(label);
        lv_obj_align(label, LV_ALIGN_TOP_LEFT, x, y);     
        return label;   
    }


    void GuiDisplay::playDailySchedule(){
        lock_guard<std::mutex> lock(m_dailyLock);
        m_dailySchedule = true;
    }

    void GuiDisplay::showDailySchedule(){
        lock_guard<std::mutex> lock(m_dailyLock);
        if(m_dailySchedule){
            DailySchedule::Instance()->DailyTransactionPage();
            m_dailySchedule = false;
        }
    }


    void GuiDisplay::run(){
        /*Handle LitlevGL tasks (tickless mode)*/

        int updateDuration = 3000;
        uint32_t time = 0;
        while(true) {
            if(time == updateDuration){
                time = 0;
                Update();
            }
            
            showDailySchedule();
            showWarnPrompt();
            lv_timer_handler();
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            time+=10;
        }

    }

}